import { Component } from '@angular/core';
import { NavController, AlertController} from 'ionic-angular';
import { OrderService } from '../../../../providers/order-service';
import { AuthService } from '../../../../providers/auth-service';
import { OrderPublicPage } from '../public/public'
import { OrderModifyPage} from '../modify/modify' 

import { ScanPage } from '../scan/scan'
import { OrderDetailPage } from '../detail/detail'
import {OrderCommentPage} from '../comment/comment'


import { DictionaryService } from '../../../../providers/dictionary-service';
import { OrderPhotoServices } from '../../../../providers/photo-service';



@Component({
  selector: 'page-order',
  templateUrl: 'order.html'
})

export class OrderPage {
  orderList=[]
  currentUser:any
  btnText:string
  title:string
  orderStatus = ''
  searchParam = ''
  constructor(
     public navCtrl: NavController,
     private order: OrderService,
     private auth:AuthService,
     private dict: DictionaryService,
     private photo: OrderPhotoServices,
     private alertCtrl: AlertController
    ) {
    this.currentUser  = auth.getUserInfo()
    this.btnText = this.currentUser.factory.type === 0?'发布订单':'浏览订单'
    this.title =  this.currentUser.factory.type === 0?'我的订单':'我承接的订单'
    this.loadAll()
  }
  public loadAll(){
      if (this.currentUser.factory.type === 0) {
      this.order.getByUserId(this.currentUser.user.id).then(data => {
        data.forEach(item => {
          item.typeName = this.dict.getLableByTypeAndvalue('business', item.type)
          this.photo.getPhotoByOrderId(item.id).then(data => {
            item.photos = data
          })
        })
        this.orderList = data
      })
    } else {
      this.order.getByRecipientId(this.currentUser.user.id).then(data => {
        data.forEach(item => {
          item.typeName =this.dict.getLableByTypeAndvalue('business', item.type)
          this.photo.getPhotoByOrderId(item.id).then(data => {
            item.photos = data
          })
        })
        this.orderList = data
      })
    }
  }
  public toDetailOrder(order) {
    this.navCtrl.push(OrderDetailPage, {
      order: order
    })
  }
  public modifyOrder(order) {
    if (this.currentUser.factory.type === 0) {
      this.navCtrl.push(OrderModifyPage, {
        order: order
      })
    } else {
      this.order.updateOrderStatus({
        id: order.id,
        status: 1
      }).then(data =>{
         this.showPopup('', '你已经标记订单完成')
      })
    }
  }
  public toPublicOrder() {
    if (this.currentUser.factory.type === 0) {
      this.navCtrl.push(OrderPublicPage)
    } else {
      this.navCtrl.push(ScanPage)
    }
  }
  public toCommentOrder(item) {
    this.navCtrl.push(OrderCommentPage, {
      order: item
    })
  }
  public filter() {
    return this.orderList.filter(item => {
      let statusFlag = true
      let searchParamFlag = true
      if (this.orderStatus == '' && this.searchParam =='' ) {
        return true
      }
      if (
         item.typeName.indexOf(this.searchParam) >= 0
         || item.code.indexOf(this.searchParam) >= 0
        ) {
        searchParamFlag = true
      } else {
        searchParamFlag = false
      }
      if (this.orderStatus == '0' && !item.recipient_id) {
        statusFlag = true
      } else if(this.orderStatus == '0'){
        statusFlag = false
      }
      if (this.orderStatus == '1' && item.recipient_id) {
        statusFlag = true
      } else  if(this.orderStatus == '1'){
        statusFlag = false
      }
      if (this.orderStatus == '2' && item.status == 2) {
        statusFlag = true
      } else  if(this.orderStatus == '2'){
        statusFlag = false
      }

      return searchParamFlag&&statusFlag
    })
  }
showPopup(title, text) {
    let alert = this.alertCtrl.create({
      title: title,
      subTitle: text,
      buttons: [
       {
         text: 'OK',
         handler: data => {
           this.loadAll()
         }
       }
     ]
    });
    alert.present();
  }
}
